package com.jcx.ldzj.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcx.ldzj.dao.DictionariesTypeMapper;
import com.jcx.ldzj.entity.DictionariesType;
import com.jcx.ldzj.service.IDictionariesDataService;
import com.jcx.ldzj.service.IDictionariesTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 码值类别表 服务实现类
 * </p>
 *
 * @author ld
 * @since 2021-07-19
 */
@Slf4j
@Service
public class DictionariesTypeServiceImpl extends ServiceImpl<DictionariesTypeMapper, DictionariesType> implements IDictionariesTypeService {

    @Autowired
    private IDictionariesDataService iDictionariesDataService;

    /**
     * 删除码值类型，但在删除前得查看该码值类型是否已经分配数据，如果已经分配，则不能删除
     * @param ids
     * @return
     */
    @Override
    public R removeType(String[] ids) {
        List<String> list = Arrays.asList(ids);
        int i = iDictionariesDataService.countData(list);
        if(i>0){
            return R.failed("其中有码值已经分配，请取消分配后再删除该类型");
        }
        return R.ok(this.removeByIds(list));
    }

    @Override
    public DictionariesType getdictByType(String type) {
        return this.baseMapper.getdictByType(type);
    }

    @Override
    public List<DictionariesType> listByState() {
        return this.baseMapper.listByState();
    }
}
